<?php

namespace app\company\controller;

use app\admin\model\department\Lists;
use app\admin\model\money\Change;
use app\admin\model\month\Salary;
use app\admin\model\product\Category;
use app\admin\model\product\Listo;
use app\admin\model\user\Sign;
use app\common\controller\CompanyApi;
use app\common\controller\RedisCluster;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Regist extends CompanyApi
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $pageSize = $this->request->post('page_size', 15);
        $list = \app\admin\model\product\Regist::where('user_id', $this->auth->id)
//            ->field('id,realname,mobile,area,address,status')
            ->order('id desc')
            ->paginate($pageSize);
        if ($list) {
            foreach ($list as $val) {
                $val->file_path = make_full_url($val->file_path);
                $val->reply_image = make_full_url($val->reply_image);
                $val->review_image = make_full_url($val->review_image);
            }
        }

        $this->success('登记表为', $list);
    }

    /**
     * 物资入库
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create()
    {
        $name = $this->request->post('name');
        $image = $this->request->post('image');
        $category_id = $this->request->post('category_id');
        $total = $this->request->post('total');
        $type = $this->request->post('type', 1);
        $reply_image = $this->request->post('reply_image');
        $remark = $this->request->post('remark');
        $file_path = $this->request->post('file_path');
        if (!$name || !$image || !$category_id || !$total || !$reply_image || !$remark) {
            $this->error('物资名称，图片，类别，数量，入库说明，签名，附件缺一不可');
        }

        if (!in_array($type, [1, 2])) {
            $this->error('必须选择物品类型！');
        }

//        $row = \app\admin\model\product\Lists::where('name', $name)
//            ->where('category_id', $category_id)
//            ->find();
//        if ($row) {
//            $this->error('该物资已存在！');
//        }

        $user_id = $this->auth->id;
        $data = [
            'user_id' => $user_id,
            'name' => $name,
            'image' => $image,
            'category_id' => $category_id,
            'total' => $total,
            'type' => $type,
            'reply_image' => $reply_image,
            'remark' => $remark,
            'file_path' => $file_path,
            'status' => 1
        ];

        $ret = \app\admin\model\product\Regist::create($data);
        if (!$ret) {
            $this->error('登记失败');
        }

        $this->success('登记成功，待审批');
    }

    /**
     * 获取登记详情
     */
    public function read()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数缺失！');
        }
        $row = \app\admin\model\product\Regist::find($id);
        if (!$row) {
            $this->error('该物资登记不存在！');
        }

        $row->image = make_full_url($row->image);
        $row->file_path = make_full_url($row->file_path);
        $row->reply_image = make_full_url($row->reply_image);

        $row->review_image = $row->review_image ? make_full_url($row->review_image) : '';

        $user = \app\admin\model\User::field('username,nickname')->find($row->user_id);
        $row->user = $user;

        $this->success('登记详情', $row);
    }

    /**
     * 入库审批
     */
    public function audit()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数缺失！');
        }
        $row = \app\admin\model\product\Regist::find($id);
        if (!$row) {
            $this->error('该物资登记不存在！');
        }

        if ($row->status != 1) {
            $this->error('该物资已审批！');
        }

        $userId = $this->auth->id;
        $user = \app\admin\model\department\User::find($userId);
        if ($user->is_manager != 1 && $user->is_leader != 1) {
            $this->error('无审批权限！');
        }

        if ($user->is_leader == 1 && $user->is_manager != 1) {
            $department_id = Category::where('id', $row->category_id)->value('department_id');
            if ($user->department_id != $department_id) {
                $this->error('非法操作！');
            }
        }

        $review_image = $this->request->post('review_image');
        $review_remark = $this->request->post('review_remark');
        $status = $this->request->post('status');
        if (!$review_image) {
            $this->error('审核人员必须签名！');
        }

        if (!in_array($status, [5, 10])) {
            $this->error('审核错误！');
        }

        Db::startTrans();
        try {

            $row->review_id = $userId;
            $row->review_image = $review_image;
            $row->review_remark = $review_remark;
            $row->status = $status;
            $ret = $row->save();

            $rowInfo = \app\admin\model\product\Lists::where('name', $row->name)
                ->where('category_id', $row->category_id)
                ->find();
            if ($rowInfo) {
                $rowInfo->total += $row->total;
                $rowInfo->stock += $row->total;
                $rowInfo->save();
            } else {
                $ldata = [
                    'name' => $row->name,
                    'image' => $row->image,
                    'total' => $row->total,
                    'stock' => $row->total,
                    'category_id' => $row->category_id,
                ];

                $rowInfo = \app\admin\model\product\Lists::create($ldata);
            }

            $orderNum = create_order($row->id);
            $department_id = Category::where('id', $row->category_id)->value('department_id');
            $mdata = [
                'order_num' => $orderNum,
                'record_type' => 1,
                'product_id' => $rowInfo->id,
                'change_type' => 1,
                'moneytype' => $row->type,
                'number' => $row->total,
                'hasnumber' => $rowInfo->stock,
//                'remark' => $tag,
                'user_id' => 0,
                'relation_id' => $row->id,
                'targetuserid' => $department_id
            ];

            Change::create($mdata);

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if (!$ret) {
            $this->error('审核失败');
        }

        $this->success('审核成功！');
    }
}
